const { getDB } = require("../db");
const jwt = require("jsonwebtoken");

module.exports = async (ctx) => {
    const { username, password } = ctx.request.body;

    const sql = "SELECT * FROM USERS WHERE username=? AND password=?";
    const [row] = await getDB().execute(sql, [username, password])

    const userInfo = row[0];

    if (userInfo) {
        const token = jwt.sign({ uId: userInfo.id }, "mytoken")
        ctx.body = {
            status: 200,
            message: "登录成功",
            meta: {
                token
            }
        }
    } else {
        ctx.body = {
            status: 0,
            message: "登录失败"
        }
    }
}